var main = {

    /**
     * 数据转换
     * @param jsonAll 所有json
     * @param result 筛选的第一层json
     * @returns {{}}
     */
    toData: function (jsonAll, filterResult) {
        var array = [];
        //处理第1级
        _.forEach(jsonAll, function (item_1) {
            if (item_1.menuId != null) {
                return;
            }
            var obj_1 = {
                id: item_1.id,
                iconName: item_1.image || "",
                text: item_1.name,
                icon: "&#xe613;",
                href: (item_1.url == "#") ? "" : item_1.url
            };
            if (item_1.icon !== undefined) {
                obj_1["icon"] = item_1.icon;
            }
            // 处理第2级
            var result_2 = item_1.data;
            if (result_2.length > 0) {
                var arr2 = [];
                _.forEach(result_2, function (item_2) {
                    var obj2 = {text: item_2.name, href: (item_2.url == "#") ? "" : item_2.url};

                    //处理第3级
                    var result_3 = item_2.data;
                    if (result_3.length > 0) {
                        var arr3 = [];
                        _.forEach(result_3, function (item_3) {
                            var obj3 = {text: item_3.name, href: (item_3.url == "#") ? "" : item_3.url};
                            arr3.push(obj3)
                        });
                        obj2["children"] = arr3;
                    }
                    arr2.push(obj2)
                });
                obj_1["subset"] = arr2;
            }
            array.push(obj_1);
        });

        var data = {};
        data["data"] = array;
        // console.log(JSON.stringify(data));
        return data;
    },


    /**
     * 加载菜单
     * @param options {fmsUtil:公共组件，vipNav：vipNav组件}
     */
    initMenu: function (options) {
        var fmsUtil = options.fmsUtil;
        var menuData;
        var data=options.menuData;
        // console.log(data)

        if(data){
            if (data.isSuccess == true) {
                var jsonAll = data.data;
                var filterResult = _.where(jsonAll, {menuId: 0});
                menuData = main.toData(jsonAll, filterResult);
                // console.log(JSON.stringify(menuData));
                options.vipNav.mainAjax(menuData, 'side-main', true)
            } else {
                fmsUtil.msg(data.description);
            }
        }else{
            fmsUtil.doAjax({
                load: false,
                action: "",
                data: {
                    "userId": options.userId
                },
                type: 'get',//请求方式
                success:function (data) {
                    if (data.isSuccess == true) {
                        var jsonAll = data.data;
                        var filterResult = _.where(jsonAll, {menuId: 0});
                        menuData = main.toData(jsonAll, filterResult);
                        // console.log(JSON.stringify(menuData));
                        options.vipNav.mainAjax(menuData, 'side-main', false);

                        $(".layui-nav>li>a").each(function () {
                            var icon = $(this).data('icon');
                            $(this).find("i").css("background-image","url(frame/static/image/"+icon+".png)");
                        })

                    } else {
                        fmsUtil.msg(data.description);
                    }
                }
            });
        }
        
        /*fmsUtil.doAjax({
            load: false,
            action: "roleService.getMenu",
            data: {
                "userId": options.userId,
                "systemSourceId": API.systemSourceId()
            },
            type: 'get',//请求方式
            success:function (data) {
                if (data.isSuccess == true) {
                    var jsonAll = data.data;
                    var filterResult = _.where(jsonAll, {menuId: 0});
                    menuData = main.toData(jsonAll, filterResult);
                    //console.log(JSON.stringify(menuData));
                    options.vipNav.mainAjax(menuData, 'side-main', false)
                } else {
                    fmsUtil.msg(data.description);
                }
            }
        });*/
    },
    init: function () {
        if (screen.width < 1366) {
            //$(".my-body").addClass("layui-overflow-x");
            $(".my-body").css({"overflow-x": "auto"});
        }
    },
    mask: function () {
        // fmsUtil.doAjax({
        //     action: "  ",
        //     type: "post",
        //     success: function (data) {
        //         if (data.isSuccess == true) {
        //             var lockTime = data.data.lockScreenTime;
        //             var time = fmsUtil.accMul(lockTime,60000);
        //             // console.log(time )
        //             setTimeout(function(){
        //                 $(".mask",parent.window.document).css("display","block");
        //                 User.method().delUser();
        //                 // document.oncontextmenu = function () {
        //                 //     return false;
        //                 // }
        //             },time)
        //         } else {
        //             // fmsUtil.msg(data.description)
        //         }
        //     }
        // });
    }
};

